package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

import static com.huike.clues.domain.TbClue.StatusType.FOLLOWING;

import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Autowired
    TbClueMapper tbClueMapper;

    /**
     * 查询线索跟进记录列表
     * @param clueId 线索
     * @return
     */
    @Override
    public List<TbClueTrackRecord> list(Long clueId) {
        return tbClueTrackRecordMapper.list(clueId);
    }

    /**
     * 添加线索跟进记录
     * @param clueTrackRecordVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ClueTrackRecordVo clueTrackRecordVo) {

        //创建线索跟进对象
        TbClueTrackRecord tbClueTrackRecord=new TbClueTrackRecord();

           //将ClueTrackRecordVos拷贝封装到TbClueTrackRecord线索跟进实体类中
        BeanUtils.copyProperties(clueTrackRecordVo,tbClueTrackRecord);

        //补充跟进人时间参数和当前登陆人
        tbClueTrackRecord.setCreateBy(SecurityUtils.getUsername());
        tbClueTrackRecord.setCreateTime(new Date());
        //调用mapper层添加线索跟进参数
        tbClueTrackRecordMapper.add(tbClueTrackRecord);

        TbClue tbClue=new TbClue();
        //将ClueTrackRecordVos拷贝封装到TbClue线索跟进实体类中
        BeanUtils.copyProperties(clueTrackRecordVo,tbClue);

        //补充线索ID和线索状态
        tbClue.setId(clueTrackRecordVo.getClueId());
        tbClue.setStatus(FOLLOWING.getValue());
        //修改线索信息
        tbClueMapper.updateTbClue(tbClue);
    }
}
